package com.offcn.webui.controller;

import com.offcn.dycommon.response.AppResponse;
import com.offcn.webui.service.OrderServiceFeign;
import com.offcn.webui.vo.resp.OrderFormInfoSubmitVo;
import com.offcn.webui.vo.resp.ReturnPayConfirmVo;
import com.offcn.webui.vo.resp.TOrder;
import com.offcn.webui.vo.resp.UserRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class OrderController {

    @Autowired
    private OrderServiceFeign orderServiceFeign;

    @RequestMapping("/order/save")
    public String submitOrder(OrderFormInfoSubmitVo vo, HttpSession session){

        //从session获取当前登录用户信息
     UserRespVo userRespVo= (UserRespVo) session.getAttribute("sessionMember");

       //判断当前登录用户信息是否为空
        if(userRespVo==null){
            return "redirect:login.html";
        }

        //从用户信息获取token
        String accessToken = userRespVo.getAccessToken();

        //设置token到vo
        vo.setAccessToken(accessToken);
        //从session获取项目回报确认信息
     ReturnPayConfirmVo returnPayConfirmVo= (ReturnPayConfirmVo) session.getAttribute("returnConfirmSession");
       //判断项目回报确认信息是否为空
        if(returnPayConfirmVo==null){
            return "redirect:/login.html";
        }

        //项目编号
        vo.setProjectid(returnPayConfirmVo.getProjectId());
        //项目回报编号
        vo.setReturnid(returnPayConfirmVo.getId());
        //项目支持的数量
        vo.setRtncount(returnPayConfirmVo.getNum());

        //调用订单服务，保存订单
        AppResponse<TOrder> appResponse = orderServiceFeign.createOrder(vo);

        TOrder order = appResponse.getData();

        System.out.println("订单编号:"+order.getOrdernum()+" money:"+order.getMoney()+" 项目名称:"+returnPayConfirmVo.getProjectName());

        //跳转项目列表页面
        return "member/minecrowdfunding";
    }
}
